package org.TCPcase3;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
//        服务端先绑定端口等待客户端连接
        ServerSocket serverSocket=new ServerSocket(10086);
        Socket accept = serverSocket.accept();
//        从连接中获取读到的上传文件并传入指定位置
        BufferedInputStream bufferedInputStream=new BufferedInputStream(accept.getInputStream());
        BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream("C:\\Users\\夏明毅\\Desktop\\java课程代码\\mySocketnet\\serverDir\\xmy.text"));
        byte [] data=new byte[1024];
        int len=0;
        System.out.println("读取上传文件数据");
        while ((len=bufferedInputStream.read(data))!=-1){
            bufferedOutputStream.write(data,0,len);
            bufferedOutputStream.flush();
            System.out.println(new String(data));
        }
//        将数据读完后给客户端反馈
        String str="数据上传成功";
       BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
       bw.write(str);
       bw.newLine();
       bw.flush();
       accept.shutdownOutput();
       accept.close();
       serverSocket.close();
       bufferedOutputStream.close();
    }
}
